<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>题库</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: #f8f9fa;
            color: #333;
            line-height: 1.6;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
            padding: 20px;
        }

        h1,
        h2 {
            color: #343a40;
            margin-bottom: 20px;
        }

        /* 表单样式 */
        .form-group {
            background-color: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
        }

        .form-group h2 {
            font-size: 22px;
            margin-bottom: 20px;
        }

        .form-control {
            width: 100%;
            padding: 12px 15px;
            font-size: 16px;
            border: 1px solid #ddd;
            border-radius: 5px;
            transition: border-color 0.3s;
            margin-bottom: 15px;
        }

        .form-control:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
        }

        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 12px 20px;
            font-size: 16px;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }

        /* 结果区域样式 */
        #result-container {
            background-color: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            margin-top: 30px;
        }

        #result-title {
            font-size: 22px;
            margin-bottom: 20px;
            color: #343a40;
        }

        #result {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            font-family: monospace;
            white-space: pre-wrap;
            word-break: break-all;
            line-height: 1.5;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }

            .form-control,
            button {
                padding: 10px;
                font-size: 14px;
            }
        }

        /* 页面顶部样式 */
        header {
            background-color: #007bff;
            color: white;
            padding: 20px 0;
            text-align: center;
            margin-bottom: 30px;
            border-radius: 0 0 10px 10px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        header h1 {
            font-size: 28px;
            margin: 0;
        }

        /* 页脚样式 */
        footer {
            text-align: center;
            margin-top: 50px;
            padding: 20px 0;
            color: #6c757d;
            font-size: 14px;
            border-top: 1px solid #dee2e6;
        }

        /* 按钮组样式 */
        .button-group {
            display: flex;
            gap: 15px;
        }

        .button-group button {
            flex: 1;
        }
    </style>
</head>

<body>
    <header>
        <h1>题库系统</h1>
    </header>

    <div class="container">
        <!-- 搜索框 -->
        <div class="form-group">
            <h2>搜索题目</h2>
            <form method="GET" action="search_question.php" id="search-form">
                <input type="text" name="keyword" class="form-control" placeholder="输入题目关键词" required>
                <button type="submit" class="button-group">搜索</button>
                <button type="reset" class="button-group" onclick="location.reload()">重置</button>
            </form>
        </div>

        <!-- 上传题库 -->
        <div class="form-group">
            <h2>上传题库</h2>
            <form method="POST" action="upload_questions.php" enctype="multipart/form-data">
                <input type="file" name="json_file" class="form-control" accept=".json" required>
                <button type="submit">上传题库</button>
            </form>
        </div>

        <!-- 搜索结果 -->
        <div id="result-container">
            <h2 id="result-title">搜索结果</h2>
            <pre id="result"></pre>
        </div>
    </div>



    <script>
        // 如果有搜索结果，显示在页面上
        if (window.location.search) {
            const urlParams = new URLSearchParams(window.location.search);
            const keyword = urlParams.get('keyword');
            if (keyword) {
                fetch(`search_question.php?keyword=${encodeURIComponent(keyword)}`)
                    .then(response => response.json())
                    .then(data => {
                        document.getElementById('result').textContent = JSON.stringify(data, null, 2);
                    })
                    .catch(error => {
                        document.getElementById('result').textContent = JSON.stringify({
                            "code": 0,
                            "data": {
                                "question": "无",
                                "answer": "网络请求错误"
                            },
                            "message": "请求失败"
                        }, null, 2);
                    });
            }
        }

        // 防止表单提交时页面刷新
        document.getElementById('search-form').addEventListener('submit', function (event) {
            event.preventDefault();
            const formData = new FormData(this);
            const keyword = formData.get('keyword');

            fetch(`search_question.php?keyword=${encodeURIComponent(keyword)}`)
                .then(response => response.json())
                .then(data => {
                    document.getElementById('result').textContent = JSON.stringify(data, null, 2);
                })
                .catch(error => {
                    document.getElementById('result').textContent = JSON.stringify({
                        "code": 0,
                        "data": {
                            "question": "无",
                            "answer": "网络请求错误"
                        },
                        "message": "请求失败"
                    }, null, 2);
                });
        });
    </script>
</body>

</html>